package cn.kgc.tangcco.kjde1020.mall.service.impl;

import cn.kgc.tangcco.kjde1020.mall.dao.ProductMapper;
import cn.kgc.tangcco.kjde1020.mall.pojo.Product;
import cn.kgc.tangcco.kjde1020.mall.service.ICategoryService;
import cn.kgc.tangcco.kjde1020.mall.service.IProductService;
import cn.kgc.tangcco.kjde1020.mall.vo.ProductDetailVo;
import cn.kgc.tangcco.kjde1020.mall.vo.ProductVo;
import cn.kgc.tangcco.kjde1020.mall.vo.ResponseVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.kgc.tangcco.kjde1020.mall.enums.ProductStatusEnum.DELETE;
import static cn.kgc.tangcco.kjde1020.mall.enums.ProductStatusEnum.OFF_SALE;
import static cn.kgc.tangcco.kjde1020.mall.enums.ResponseEnum.USERNAME_OR_PASSWORD_ERROR;

/**
 * @ClassName ProductServiceImpl 郭建宾
 * @Description: TODO
 * @Author ASUS
 * @Date 2020/2/29
 * @Version V1.0
 **/
@Service
@Slf4j
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private ProductMapper productMapper;
    @Override
    public ResponseVo<PageInfo> list(Integer categoryId, Integer pageNum, Integer pageSize) {
        Set<Integer> categoryIdSet = new HashSet<>();
        if (categoryId != null){
            categoryService.findSubCategoryId(categoryId,categoryIdSet);
            categoryIdSet.add(categoryId);
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Product> productList = productMapper.selectByCategoryIdSet(categoryIdSet);
               List<ProductVo>productVoList = productList.stream()
                .map(e -> {
                    ProductVo productVo = new ProductVo();

                    BeanUtils.copyProperties(e, productVo);
                    return productVo;
                })
                .collect(Collectors.toList());
                 PageInfo pageInfo = new PageInfo<>(productList);
                 pageInfo.setList(productVoList);
                return ResponseVo.success(pageInfo );
    }

    @Override
    public ResponseVo<ProductDetailVo> detail(Integer productId) {
        Product product = productMapper.selectByPrimaryKey(productId);
        // 只对确定性条件判断
        if (product.getStatus().equals(OFF_SALE.getCode()) ||product.getStatus().equals(DELETE.getCode()) ){
            return ResponseVo.error(USERNAME_OR_PASSWORD_ERROR);
        }
        ProductDetailVo productDetailVo = new ProductDetailVo();
        BeanUtils.copyProperties(product,productDetailVo);
        /**
         * 隐藏库存数据处理
         * 如果购买100件以上 才能显示 Stock库存
         */
        productDetailVo.setStock(product.getStock() >100 ? 100 : product.getStock());

        return ResponseVo.success(productDetailVo);
    }
}
